package jsyf.oa.web.wreport;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.wreport.ModuleFacade;
import jsyf.oa.facade.wreport.Template2ModuleFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Lenovo on 2017/12/11.
 */
@Api(value = "", description = "")
@Controller
@RequestMapping("/module")
public class ModuleController {
    @Autowired
    private ModuleFacade moduleFacade;
    @Autowired
    private Template2ModuleFacade template2ModuleFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData)  {
        String result = moduleFacade.save(jsonData);
        if(!StringUtils.isEmpty(result)){
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id",result);
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonResult));
        }else{
            ResponseUtils.putJsonResponse(response,BaseUtils.errorrRespond("保存失败！"));
        }
    }


    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = moduleFacade.removeById(jsonParam.getString("id"));
        if(result){
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        }else{
            ResponseUtils.putJsonResponse(response,BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/getTreeNodes.json", method = RequestMethod.POST)
    public void getTreeNodes(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> result = moduleFacade.getTreeNodes();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/getModuleENamesByUserId.json", method = RequestMethod.POST)
    public void getModuleENamesByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj_params = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = obj_params.getString("userId");
        List<JSONObject> result = moduleFacade.getModuleENamesByUserId(userId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    public void getById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj_params = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = obj_params.getString("id");
        JSONObject result = moduleFacade.getById(id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }

}
